package com.zxt.content.copywriting.events.upload;

import com.zxt.bean.ServiceException;
import com.zxt.bean.ResultCode;
import com.zxt.utils.ZXTCollectionsUtil;
import com.zxt.content.copywriting.manager.CopywritingTypeRelationManager;
import com.zxt.content.copywriting.manager.PrivateCopywritingManager;
import com.zxt.content.copywriting.model.CopywritingTypeRelation;
import com.zxt.content.copywriting.model.PrivateCopywriting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 上传文案到公共文案 校验私有文案完整性
 *
 * @author zxt
 */
@Component
@Slf4j
@Order(1) //设置执行顺序的排序
public class PrivateCopywritingCheckListener implements ApplicationListener<UploadCopywritingEvent> {

    private final PrivateCopywritingManager privateCopywritingManager;
    private final CopywritingTypeRelationManager copywritingTypeRelationManager;

    public PrivateCopywritingCheckListener(PrivateCopywritingManager privateCopywritingManager, CopywritingTypeRelationManager copywritingTypeRelationManager) {
        this.privateCopywritingManager = privateCopywritingManager;
        this.copywritingTypeRelationManager = copywritingTypeRelationManager;
    }

    @Override
    public void onApplicationEvent(UploadCopywritingEvent uploadCopywritingEvent) {
        log.info("上传文案到公共文案--校验私有文案完整性");
        UploadCopywritingEventContext context = uploadCopywritingEvent.getContext();

        /**
         * 私有
         */
        List<Long> copywritingIds = context.getCopywritingIds();
        if (ZXTCollectionsUtil.isEmpty(copywritingIds)) {
            throw new ServiceException(ResultCode.COMMON_MESSAGE, "请选择需要上传的私人文案");
        }

        List<PrivateCopywriting> privateCopywritings = privateCopywritingManager.listByIds(copywritingIds);
        Map<Long, PrivateCopywriting> privateCopywritingMap = Optional.ofNullable(privateCopywritings).orElse(new ArrayList<>()).stream().collect(Collectors.toMap(PrivateCopywriting::getId, Function.identity()));

        if (ZXTCollectionsUtil.isEmpty(privateCopywritings)) {
            throw new ServiceException(ResultCode.COMMON_MESSAGE, "上传的私人文案不存在");
        }

        List<CopywritingTypeRelation> copywritingTypeRelations = copywritingTypeRelationManager.getByPrivateCopywritingIds(copywritingIds);
        //根据私人文案id进行分组，拿到关于文案的所有关联关系
        Map<Long, List<CopywritingTypeRelation>> copywritingTypeRelationGroup = Optional.ofNullable(copywritingTypeRelations).orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(CopywritingTypeRelation::getCopywritingId));


        for (Long copywritingId : copywritingIds) {
            PrivateCopywriting privateCopywriting = privateCopywritingMap.get(copywritingId);
            if (Objects.isNull(privateCopywriting)) {
                throw new ServiceException(ResultCode.COMMON_MESSAGE, "上传的私人文案不存在,id:" + copywritingId);
            }

            List<CopywritingTypeRelation> typeRelations = copywritingTypeRelationGroup.get(copywritingId);
            if (ZXTCollectionsUtil.isEmpty(typeRelations)) {
                throw new ServiceException(ResultCode.COMMON_MESSAGE, "文案:" + privateCopywriting.getTheme() + " 至少需要拥有一种文案类型");
            }
        }

        uploadCopywritingEvent.getContext().setPrivateCopywritings(privateCopywritings);
        uploadCopywritingEvent.getContext().setCopywritingTypeRelations(copywritingTypeRelations);
    }
}
